<?php

use yii\db\Migration;

class m130524_201442_init extends Migration
{
    public function up()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ENGINE=InnoDB';
        }
        /*if ($this->db->getTableSchema('{{%user}}', true) === null) {
            $this->createTable('{{%user}}', [
                'id' => $this->primaryKey(),
                'username' => $this->string(50)->notNull(),
                'auth_key' => $this->string(32)->notNull(),
                'password_hash' => $this->string()->notNull(),
                'password_reset_token' => $this->string()->unique(),
                //'email' => $this->string()->notNull()->unique(),

                'status' => $this->smallInteger()->notNull()->defaultValue(1),
                'created_at' => $this->integer()->notNull(),
                'updated_at' => $this->integer()->notNull(),
            ], $tableOptions);
        }*/

		//先插入rbac的基基础表结构，插入菜单表
		// CREATE TABLE `fs_menu` (
		// 	`id` INT(11) NOT NULL AUTO_INCREMENT,
		// 	`name` VARCHAR(128) NOT NULL,
		// 	`parent` INT(11) DEFAULT NULL,
		// 	`route` VARCHAR(255) DEFAULT NULL,
		// 	`order` INT(11) DEFAULT NULL,
		// 	`data` BLOB,
		// 	PRIMARY KEY (`id`),
		// 	KEY `parent` (`parent`),
		// 	CONSTRAINT `fs_menu_ibfk_1` FOREIGN KEY (`parent`) REFERENCES `mo_menu` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
		//   ) ENGINE=INNODB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8


		$this->insert('{{%auth_item}}', [
			'name' => '/*',
			'type' => 2,
			'created_at' => time(),
			'updated_at' => time(),
		]);
		$this->insert('{{%auth_item}}', [
			'name' => 'super',
			'type' => 1,
			'created_at' => time(),
			'updated_at' => time(),
		]);

		/*$this->batchInsert('{{%auth_item}}', ['name', 'type', 'created_at', 'updated_at'], [
			''
		]);*/

		$this->insert('{{%auth_item_child}}', [
			'parent' => 'super',
			'child' => '/*'
		]);
		$this->insert('{{%auth_assignment}}', [
			'item_name' => 'super',
			'user_id' => 1,
			'created_at' => time(),
		]);
		$this->insert('{{%menu}}', [
			'name' => '权限',
			'route' => '/admin/default/index',
			'order' => 100,
			'data' => '{"icon": "lock text-aqua", "visible": true}'
		]);

		$this->batchInsert('{{%menu}}', ['name', 'parent', 'route', 'order'], [
			['路由节点', '1', '/admin/route/index', 1],
			['权限列表', '1', '/admin/permission/index', 2],
			['角色列表', '1', '/admin/role/index', 3],
			['分配', '1', '/admin/assignment/index', 4],
			['菜单列表', '1', '/admin/menu/index', 5],
			['管理员列表', '1', '/gerent/index', 6],
		]);
    }

    public function down()
    {
        $this->dropTable('{{%user}}');
    }
}
